package com.term.service.enums;


import com.term.domain.enums.BaseEnum;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;

import java.io.IOException;

/**
 * 枚举JsonSerializer.
 *
 * @author 潘瑞峥
 * @date 2014年8月27日
 */
@SuppressWarnings( "rawtypes" )
public class EnumSerializer extends JsonSerializer<BaseEnum> {

    /**
     * @see com.fasterxml.jackson.databind.JsonSerializer#serialize(Object,
     *      com.fasterxml.jackson.core.JsonGenerator,
     *      com.fasterxml.jackson.databind.SerializerProvider)
     */
    @Override
    public void serialize(BaseEnum value, JsonGenerator jgen, SerializerProvider provider ) throws IOException, JsonProcessingException {
        if ( null != value ) {
            jgen.writeStartObject();
            jgen.writeFieldName( "value" );
            jgen.writeObject( value.getValue() );
            jgen.writeFieldName( "displayName" );
            jgen.writeString( value.getDisplayName() );
            jgen.writeEndObject();
        } else {
            jgen.writeNull();
        }
    }

}
